/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles.desktop;

import com.googlecode.maurersmarbles.Screen;
import com.googlecode.maurersmarbles.drawables.Drawable;
import java.awt.Graphics;
import java.util.Collection;
import javax.swing.JComponent;

/**
 *
 * @author Daniel Uppenkamp
 */
public class DesktopScreen extends JComponent implements Screen {

    /**
     * The lock that is used to control access to the drawables collection.
     */
    protected static final Object DRAWABLES_LOCK = new Object();
    /**
     * The collection of drawables that are drawn on the screen.
     */
    protected Collection<Drawable> drawables;
    /**
     * The screen canvas that is used to draw the drawables.
     */
    protected DesktopScreenCanvas desktopScreenCanvas = new DesktopScreenCanvas();

    /**
     * Creates a new DesktopScreen.
     */
    public DesktopScreen() {
        // do nothing
    }

    @Override
    public void draw(Collection<Drawable> drawables) {

        synchronized (DRAWABLES_LOCK) {
            this.drawables = drawables;
        }

        //this.paintImmediately(0, 0, getWidth(), getHeight());
        this.repaint();

    }

    @Override
    protected void paintComponent(Graphics g) {

        desktopScreenCanvas.setGraphics(g);

        synchronized (DRAWABLES_LOCK) {
            if (drawables != null) {
                for (Drawable drawable : drawables) {
                    drawable.draw(desktopScreenCanvas);
                }
            }
        }

    }
}
